package com.xjtu.repository;

import com.xjtu.common.constant.Chat;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;


@Repository
public class ChatRepository {
    @Resource
    private MongoTemplate mongoTemplate;


    //默认使用单聊集合进行存储
    public void save(com.xjtu.model.Chat chat) {
        save(chat, Chat.SINGLE_CHAT_COLLECTION);
    }

    //选择集合进行存储
    public void save(com.xjtu.model.Chat chat, String collectionName) {
        mongoTemplate.insert(chat, collectionName);
    }

    //选择集合进行删除
    public void delete(Query query, String collectionName) {
        mongoTemplate.remove(query, com.xjtu.model.Chat.class, collectionName);
    }

    //默认使用群聊集合进行查找
    public List<com.xjtu.model.Chat> find(Query query) {
        return find(query, Chat.SINGLE_CHAT_COLLECTION);
    }

    //选择集合进行查找
    public List<com.xjtu.model.Chat> find(Query query, String collectionName) {
        return mongoTemplate.find(query, com.xjtu.model.Chat.class, collectionName);
    }

    public List<com.xjtu.model.Chat> findAll() {
        return mongoTemplate.findAll(com.xjtu.model.Chat.class);
    }

    public Page<com.xjtu.model.Chat> findByPage(Pageable pageable, Query query) {
        return null;
    }

    public void update(Query query, Update update) {
        mongoTemplate.updateFirst(query, update, com.xjtu.model.Chat.class);
    }
}
